package qingxin.servlet.client.check;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import qingxin.dao.TokenDAO;
import qingxin.factory.DAOFactory;

@WebServlet("/client/token")
public class CheckServlet extends HttpServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public TokenDAO tokenDAO;
	
	public CheckServlet(){
		tokenDAO = DAOFactory.getTokenDAO();
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
    	response.setContentType("text/html; charset=utf-8");
    	response.setCharacterEncoding("utf-8");
		PrintWriter writer = response.getWriter();
							
		try{
			String token = request.getParameter("token");
			
			if(!tokenDAO.isExist(token)){
				tokenDAO.add(token);
			}
			
		}
		catch(Exception e){
			response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
			writer.println("{\"msg\":\"wrong\"}");
		}
		finally{
			writer.flush();
			writer.close();
		}
	}
	
	
}
